package ch.rheintalconnection.sysventory.presentation.panels;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ch.rheintalconnection.sysventory.application.PCAttribute;
import ch.rheintalconnection.sysventory.application.PCDatas;
import ch.rheintalconnection.sysventory.businessServices.DataService;
import ch.rheintalconnection.sysventory.domain.ScanEntry;
import ch.rheintalconnection.sysventory.technicalServices.IDataReader;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.DateField;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;

public class StatisticModifyedComputersPanel extends VerticalLayout implements Button.ClickListener{

	private static final long serialVersionUID = -2307719825986707844L;

	private DateField dateFieldFrom;
	private DateField dateFieldTo;
	private TablePanel tablePanel;
	private Button btnSearch;
	private DataService dataService;
	private IDataReader dataReader;
	
	public StatisticModifyedComputersPanel(DataService dataService, IDataReader dataReader){
		
		this.dataService = dataService;
		this.dataReader = dataReader;
		
		HorizontalLayout dateLayout = new HorizontalLayout();
		this.addComponent(dateLayout);
		dateFieldFrom = new DateField("Datum von");
		dateFieldFrom.setDateFormat("dd.MM.yyyy");
		dateFieldTo = new DateField("Datum bis");
		dateFieldTo.setDateFormat("dd.MM.yyyy");
		
		dateLayout.addComponent(dateFieldFrom);
		dateLayout.addComponent(dateFieldTo);
		
		btnSearch = new Button("Suchen");
		btnSearch.addListener(this);
		this.addComponent(btnSearch);

	}

	
	@Override
	public void buttonClick(ClickEvent event) {
		
		if(tablePanel != null){
			this.removeComponent(tablePanel);
		}
		
		tablePanel = new TablePanel("Computer",  dataReader, false);
		tablePanel.setSizeFull();
		this.addComponent(tablePanel);
		
		Date fromDate = (Date)dateFieldFrom.getValue();
		Date tillDate = (Date)dateFieldTo.getValue();
		
		List<ScanEntry> scanEntries= dataService.getScanEntriesFromChangedComputers(fromDate, tillDate);
		
		List<PCAttribute> displayAttributes = getDisplayAttributes();
		List<PCDatas> pcDatasList = new ArrayList<PCDatas>();
		
		for (ScanEntry scanEntry : scanEntries) {
			pcDatasList.add(new PCDatas(scanEntry));
		}
		
		tablePanel.initializeTable();
		tablePanel.fillTable(displayAttributes, pcDatasList);
	}
	
	private List<PCAttribute> getDisplayAttributes(){
		
		List<PCAttribute> displayAttributes = new ArrayList<PCAttribute>();
		
		displayAttributes.add(PCAttribute.ScanDate);
		displayAttributes.add(PCAttribute.Programs);
		displayAttributes.add(PCAttribute.Printers);
		displayAttributes.add(PCAttribute.OSName);
		displayAttributes.add(PCAttribute.OSVersion);
		displayAttributes.add(PCAttribute.OSTechnology);
		displayAttributes.add(PCAttribute.CPUDesc);
		displayAttributes.add(PCAttribute.CPUFamily);
		displayAttributes.add(PCAttribute.CPUCores);
		displayAttributes.add(PCAttribute.CPUClockrate);
		displayAttributes.add(PCAttribute.PCName);
		displayAttributes.add(PCAttribute.PCRAM);
		displayAttributes.add(PCAttribute.PCType);
		displayAttributes.add(PCAttribute.PCHD);
		displayAttributes.add(PCAttribute.NICIP);
		displayAttributes.add(PCAttribute.NICMacAddress);
		displayAttributes.add(PCAttribute.NICDesc);
		displayAttributes.add(PCAttribute.NICGateway);
		displayAttributes.add(PCAttribute.NICDHCP);
		displayAttributes.add(PCAttribute.NICSubnet);
		return displayAttributes;
	}
	

}
